/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2007. All Rights Reserved.
 * 
 * Note to U.S. Government Users Restricted Rights:
 * Use, duplication or disclosure restricted by GSA ADP Schedule
 * Contract with IBM Corp. 
 *******************************************************************************/
package com.example.jazztalk.client.internal;

import org.eclipse.core.runtime.IProgressMonitor;

import com.example.jazztalk.client.IJazzTalkClientLibrary;
import com.example.jazztalk.common.IJazzTalkService;
import com.example.jazztalk.common.IJazzTalkService.NewMessageParameters;
import example.jazztalk.common.model.Message;
import com.ibm.team.repository.client.util.IClientLibraryContext;
import com.ibm.team.repository.client.util.IClientLibraryContext.IServiceRunnable;
import com.ibm.team.repository.common.TeamRepositoryException;

public class JazzTalkClientLibrary implements IJazzTalkClientLibrary {

	private final IClientLibraryContext _context;

	JazzTalkClientLibrary(IClientLibraryContext context) {
		_context = context;
	}

	private IJazzTalkService getService() throws TeamRepositoryException {
		IJazzTalkService service = (IJazzTalkService) _context
				.getServiceInterface(IJazzTalkService.class);
		if (service == null) {
			throw new TeamRepositoryException("Unable to get IJazzTalkService");
		}
		return service;
	}

	public Message[] getAllMessages(IProgressMonitor monitor)
			throws TeamRepositoryException {
		IServiceRunnable<Object> runnable = new IServiceRunnable<Object>() {
			public Object run(IProgressMonitor monitor)
					throws TeamRepositoryException {
				return getService().getAllMessages();
			}
		};

		return (Message[]) _context.callCancelableService(runnable, monitor);
	}

	public Message createMessage(final String text, IProgressMonitor monitor)
			throws TeamRepositoryException {
		IServiceRunnable<Object> runnable = new IServiceRunnable<Object>() {
			public Object run(IProgressMonitor monitor)
					throws TeamRepositoryException {
				NewMessageParameters params = new NewMessageParameters();
				params.text = text;
				return getService().postNewMessage(params);
			}
		};

		return (Message) _context.callCancelableService(runnable, monitor);
	}

}
